package org.study.flyway.boot.startup;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2023年06月05日
 */
/*@Slf4j
@Configuration
@RequiredArgsConstructor
public class FlywayInit {
    private final DataSource dataSource;

    @PostConstruct
    public void migrate() {
        Flyway flyway = Flyway.configure()
                // 数据库配置
                .dataSource(dataSource)
                // 脚本目录，默认为db/migration
                .locations("db/migration")
                // 当迁移时发现目标schema非空，而且带有没有元数据的表时，是否自动执行基准迁移，默认false
                .baselineOnMigrate(true)
                // 迁移时编码
                .encoding("UTF-8")
                // 记录迁移日志表名称，默认为flyway_schema_history
                .table("flyway_schema_history")
                // 关闭clean
                .cleanDisabled(true)
                .load();
        try {
            flyway.migrate();
        } catch (FlywayException ex1) {
            log.error("Flyway配置第一次加载出错", ex1);
            try {
                // 生成版本记录表
                flyway.repair();
                log.info("Flyway配置修复成功");
                flyway.migrate();
                log.info("Flyway配置重新加载成功");
            } catch (FlywayException ex2) {
                log.error("Flyway配置第二次加载出错", ex2);
                throw ex2;
            }
        }
    }
}*/
